# -*- coding: utf-8 -*-
# !/usr/bin/env python
from PIL import Image
import os

class ImageClass():
    def picture_resize(self, source_path, purpose_path, file_epilogue, resize_small_int):
        """
        缩小图片
        @param source_path: 源目录
        @param purpose_path: 目标目录
        @param file_epilogue: 处理的文件后缀名
        @param resize_small_int: 缩小倍数
        @return:
        """

        for root, dirs, files in os.walk(source_path):

            for file in files:

                if not file.endswith('.%s' %file_epilogue):
                    continue

                file_path = os.path.join(root, file)

                now_file_path = file_path.replace(source_path, purpose_path)

                file_dir_path, file_name = os.path.split(now_file_path)

                os.makedirs(file_dir_path, exist_ok=True)

                picture_obj = Image.open(file_path)

                picture_width, picture_high = picture_obj.size
                print('原图尺寸: %sx%s' % (picture_width, picture_high))

                picture_obj.thumbnail((picture_width // resize_small_int, picture_high // resize_small_int))
                print('新尺寸: %sx%s' % (picture_width // resize_small_int, picture_high // resize_small_int))

                picture_obj.save(now_file_path, 'jpeg')

                print('%s保存成功'%now_file_path)


source_path = 'C:\\Users\wangk\Desktop\O3_ekma-O3_RIR-pNO3_ekma-pNO3_RIR'
purpose_path = 'C:\\Users\wangk\Desktop\O3_ekma-O3_RIR-pNO3_ekma-pNO3_RIR2'

image_obj=ImageClass()
image_obj.picture_resize(source_path, purpose_path, "jpg", 4)